import {
  getTeamSendTable,
  editTeamSendRecord,
  deleteTeamSendRecord
} from '../../api/sms'
import {
  TEAM_SEND_LIST,
  TEAM_SEND_DATA_COUNT,
  TEAM_SEND_CURRENT_ROW,
  TEAM_SEND_HISTORY_DATA,
  TEAM_SEND_PAGE_SIZE
} from '../motations-type'

export default {
  state: {
    teamSendList: [],
    teamSendDataCount: null,
    teamSendPageSize: 20,
    teamSendHistoryData: [],
    row: {
      ctime: '',
      srname: '',
      joindate: '',
      mname: '',
      state0: '',
      state: '',
      tprice: '',
      times: ''
    }
  },
  getters: {
    teamSendList: state => state.teamSendList
  },
  mutations: {
    [TEAM_SEND_LIST] (state, data) {
      state.teamSendList = data
    },
    [TEAM_SEND_CURRENT_ROW] (state, data) {
      state.row = data
    },
    [TEAM_SEND_DATA_COUNT] (state, data) {
      state.teamSendDataCount = data
    },
    [TEAM_SEND_HISTORY_DATA] (state, data) {
      state.teamSendHistoryData = data
    },
    [TEAM_SEND_PAGE_SIZE] (state, data) {
      state.teamSendPageSize = data
    }
  },
  actions: {
    async getTeamSendList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getTeamSendTable(data)
      resp.data.data.rows.map((item, index) => {
        newData = [...newData, {
          id: item.id,
          uid: item.uid,
          ctime: item.ctime,
          srname: item.srname,
          joindate: item.joindate,
          mname: item.mname,
          state0: item.state0,
          state: item.state,
          tprice: item.tprice,
          times: item.times,
          index: index,
          _checked: false
        }]
      })
      commit(TEAM_SEND_LIST, newData)
      commit(TEAM_SEND_DATA_COUNT, resp.data.data.total)
      if (newData.length < rootState.teamSend.teamSendPageSize) {
        commit(TEAM_SEND_HISTORY_DATA, newData)
      } else {
        commit(TEAM_SEND_HISTORY_DATA, newData.slice(0, rootState.teamSend.teamSendPageSize))
      }
      return resp
    },
    async editTeamSendData ({ commit, rootState }, playload) {
      let data = {}
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await editTeamSendRecord(data)
      return resp
    },
    async deleteTeamSendData ({ commit, rootState }, playload) {
      const resp = await deleteTeamSendRecord(playload)
      return resp
    },
    async getTeamSendCurrentRow ({ commit, rootState }, row) {
      commit(TEAM_SEND_CURRENT_ROW, row)
    },
    async changeTeamSendPage ({ commit, rootState }, index) {
      let data = rootState.teamSend.teamSendList
      let _start = (index - 1) * rootState.teamSend.teamSendPageSize
      let _end = index * rootState.teamSend.teamSendPageSize
      commit(TEAM_SEND_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeTeamSendPageSize ({ commit, rootState }, value) {
      commit(TEAM_SEND_PAGE_SIZE, value)
    }
  }
}
